<template>
    <h2>折扣券添加页面</h2>
    <table border="1">
        <tr>
            <td>折扣券名称</td>
            <td>
                <input type="text" v-model="Discount.Discount_Title"/>
            </td>
        </tr>
        <tr>
            <td>折扣券颜色</td>
            <td>
                <input type="color" v-model="Discount.Discount_Color"/>
            </td>
        </tr>
        <tr>
            <td>库存数量</td>
            <td>
                <input type="text" v-model="Discount.Discount_Num"/>
            </td>
        </tr>
        <tr>
            <td>每人限领</td>
            <td>
                <input type="text" v-model="Discount.Discount_Limited"/>
            </td>
        </tr>
        <tr>
            <td>使用条件</td>
            <td>
                <input type="text" v-model="Discount.Discount_Use"/>
            </td>
        </tr>
        <tr>
            <td>折扣</td>
            <td>
                <input type="text" v-model="Discount.Discount_Discount"/>
            </td>
        </tr>
        <tr>
            <td>开始时间</td>
            <td>
                <input type="date" v-model="Discount.Discount_StartTime"/>
            </td>
        </tr>
        <tr>
            <td>结束时间</td>
            <td>
                <input type="date" v-model="Discount.Discount_EndTime"/>
            </td>
        </tr>
        <tr>
            <td>适用场景</td>
            <td>
                <textarea rows="3"  v-model="Discount.Discount_Business"></textarea>
            </td>
        </tr>
        <tr>
            <td>使用须知</td>
            <td>
                <textarea rows="3"  v-model="Discount.Discount_Precautions"></textarea>
            </td>
        </tr>
        <tr>
            <td>客服电话</td>
            <td>
                <input type="text" v-model="Discount.Discount_Phone"/>
            </td>
        </tr>
        <tr>
            <td>门店名称</td>
            <td>
               <select v-model="Discount.Shop_Id">
                <option v-for="item in shop" :value="item.Shop_Id">{{ item.Shop_Name }}</option>
               </select>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="button" value="添加" @click="addDiscount"/>
            </td>
        </tr>
    </table>

</template>

<script setup lang="ts">
    import {ref,onMounted} from 'vue'
    import {useRouter} from 'vue-router'
    import axios from 'axios'
    const router=useRouter();
    const Discount=ref(
        {
      "Discount_Id": 1,
      "Discount_Title": "",
      "Discount_Color": "",
      "Discount_Num": null,
      "Discount_Limited": null,
      "Discount_Use": "",
      "Discount_Discount": null,
      "Discount_StartTime": "",
      "Discount_EndTime": "",
      "Discount_Business": "",
      "Discount_Precautions": "",
      "Discount_Phone": "",
      "Shop_Id": 0,
      "Discount_InfoState": true,
      "Discount_InfoStateTime": "2024-08-26T12:01:35.12"
    }
)
const addDiscount=()=>{
    axios.post('https://localhost:44320/api/Discount/DiscountAdd',{
        "Discount_Title": Discount.value.Discount_Title,
      "Discount_Color":  Discount.value.Discount_Color,
      "Discount_Num": Discount.value.Discount_Num,
      "Discount_Limited":  Discount.value.Discount_Limited,
      "Discount_Use":  Discount.value.Discount_Use,
      "Discount_Discount":  Discount.value.Discount_Discount,
      "Discount_StartTime":  Discount.value.Discount_StartTime,
      "Discount_EndTime":  Discount.value.Discount_EndTime,
      "Discount_Business": Discount.value.Discount_Business,
      "Discount_Precautions":  Discount.value.Discount_Precautions,
      "Discount_Phone":  Discount.value.Discount_Phone,
      "Shop_Id":  Discount.value.Shop_Id,
      "Discount_InfoState": true,
      "Discount_InfoStateTime": "2024-08-26T12:01:35.12"
    })
    .then(res=>{
        if(res.data==-1)
        {
            alert('优惠券名称不能重复');
            return;
        }
        else if(res.data>0)
        {
            alert('添加成功');
            router.push('/showDiscount');
        }
        else{
            alert('添加失败');
        }
    })
}

const shop=ref([{
    "Shop_Id": 7,
    "Shop_Img": "string",
    "Shop_Name": "string",
    "Shop_BranchName": "string",
    "Shop_Address": "string",
    "Shop_Phone": "string",
    "Shop_State": true,
    "Shop_Info": "string",
    "Shop_Notice": "string",
    "Shop_Traffic": "string",
    "Shop_InfoState": true,
    "Shop_InfoStateTime": "2024-08-26T12:01:35.12"
  }])
    onMounted(()=>{
        axios.get('https://localhost:44320/api/Discount/GetShop')
        .then(res=>{
            shop.value=res.data;
        })
    })
</script>